<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License 
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: Attachment.php 262 2011-07-23 15:51:14Z beimuaihui $
 */


Zend_Loader::loadClass("Baogg_Db_Table");

//class Permission extends Baogg_Db_Table {
class Attachment  extends Baogg_Db_Table {
	/**
	 * The default table name 
	 */
	protected $_name;
	protected $_db ;
	protected $_primary; 
	protected $allowed_extensions = array("txt","csv","htm","html","xml",
    "css","doc","xls","rtf","ppt","pdf","swf","flv","avi",
    "wmv","mov","jpg","jpeg","gif","png");
	
	
	function __construct() {
		$this->_name =   'attachment';
		
		$this->primary= $this->_primary='attachment_id';
		parent::__construct();		
		
	}
	
	function upload($params=array()){
		$upload_ids=array();
		
		
		if(!$_FILES){
			 return array("flag"=>false,"msg"=>"msg_no_upload");
		 }
		 if(!$params){
		 	$params=Zend_Controller_Front::getInstance()->getRequest()->getUserParams();
		 }
		 
		 $int_params=array("action"=>$params['action']);
		 foreach($params as $k=>$v){
		 	if(is_numeric($v)){
		 		$int_params[$k]=$v;
		 	}
		 }
		 
		 $dir=BAOGG_UPLOAD_DIR.'upload/'.$params['module'].'/'.$params['controller'].'/';
		 $flag=true;		 
		 if(!is_dir($dir)){
		 	$flag=mkdir($dir,0777,true);		 	
		 }
		 if(!$flag){
		 	return array("flag"=>false,"msg"=>"msg_mkdir_error");
		 }
		 foreach($_FILES as $field=>$arr_value){
		 	if(is_array($_FILES[$field]["name"])){
		 		foreach($_FILES[$field]["error"] as $key=>$error){
		 			$name  = $_FILES[$field]["name"][$key];
		 			$suffix=pathinfo ($name,PATHINFO_EXTENSION);
			 		if ($error == UPLOAD_ERR_OK ) {
			 			if(!in_array($suffix,$this->allowed_extensions)){
			 				return array("flag"=>false,"msg"=>"msg_file_type_error");
			 			}
				        $tmp_name = $_FILES[$field]["tmp_name"][$key];
				        $real_name=uniqid(date("YmdHis"));
				        move_uploaded_file($tmp_name, $dir.$real_name.'.'.$suffix);
				        
				        $item=array();
				        $item['user_id']=(int)@$_SESSION['admin']['user']['user_id'];
				        $item['module']=(int)@$params['module'];
				        $item['controller']=(int)@$params['controller'];
				        $item['params']=Zend_Json::encode($int_params);
				        $item['file_name']=$name;
				        $item['real_name']=$real_name;
				        $item['file_type']=$_FILES[$field]["type"][$key];
				        $item['error']=$_FILES[$field]["error"][$key];
				        $item['size']=$_FILES[$field]["size"][$key];
				        $item['suffix']=$suffix;
				        $item['upload_ip']=$_SERVER['REMOTE_ADDR'];
				        $attachment_id=$this->_insert($item);
				        $upload_ids[$field][]=$attachment_id;
				        
				       
				    }
		 		}
		 		 $_POST[$field][]=$upload_ids[$field];
				 Zend_Controller_Front::getInstance()->setParam($field, $upload_ids[$field]);
			        
		 	}else{
		 		$name = $_FILES[$field]["name"];
		 		$suffix=pathinfo ($name,PATHINFO_EXTENSION);
		 		if ($_FILES[$field]["error"] == UPLOAD_ERR_OK  ) {
			 		if(!in_array($suffix,$this->allowed_extensions)){
				    	return array("flag"=>false,"msg"=>"msg_file_type_error");
				    }
		 			$tmp_name = $_FILES[$field]["tmp_name"];	        
			        
			        $real_name=uniqid(date("YmdHis"));
			        move_uploaded_file($tmp_name, $dir.$real_name.'.'.$suffix);
			        
			        $item=array();
			        $item['user_id']=(int)@$_SESSION['admin']['user']['user_id'];
			        $item['module']=@$params['module'];
			        $item['controller']=@$params['controller'];
			        $item['params']=Zend_Json::encode($int_params);
			        $item['file_name']=$name;
			        $item['real_name']=$real_name;
			        $item['file_type']=$_FILES[$field]["type"];
			        $item['error']=$_FILES[$field]["error"];
			        $item['size']=$_FILES[$field]["size"];
			        $item['suffix']=$suffix;
			        $item['upload_ip']=$_SERVER['REMOTE_ADDR'];
			        $attachment_id=$this->_insert($item);
			        $upload_ids[$field]=$attachment_id;
			        
			        $_POST[$field]=$attachment_id;
			        Zend_Controller_Front::getInstance()->setParam($field, $attachment_id);
			        
			    }
		 	}
		 }
		 
		 return array("flag"=>true,"ids"=>$upload_ids);
	}
	

	function download(){
		
	}
	

	function getSame($arr) {	
		$ret=array();
		
		$where['attachment_name']=$arr['attachment_name'];
		$where['user_id']=(int)$_SESSION['admin']['user']['user_id'];
		
		return parent::getSame($where," and ");
		
	   	
	
	}
	


}
